package com.lbg.drug.service.impl;

import com.lbg.drug.commont.untils.DateUtils;
import com.lbg.drug.commont.untils.PageUtils;
import com.lbg.drug.dto.*;
import com.lbg.drug.entity.Admin;
import com.lbg.drug.entity.Deliver;
import com.lbg.drug.entity.Statistic;
import com.lbg.drug.entity.Store;
import com.lbg.drug.mapper.*;
import com.lbg.drug.service.DeliverService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lbg
 * @since 2022-04-26
 */
@Service
public class  DeliverServiceImpl implements DeliverService {


    @Autowired
    private DeliverMapper deliverMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private StockMapper stockMapper;


    @Autowired
    private StatisticMapper statisticMapper;

    @Override
    public String add(Deliverdto deliverdto, HttpSession session) {
        Deliver deliver = new Deliver();
        BeanCopier.create(Deliverdto.class, Deliver.class, false).copy(deliverdto, deliver, null);
        deliver.setDeliverTime(new Date());
        Admindto admin = (Admindto) session.getAttribute("Admin");
        Admin admin1 = adminMapper.selectById(admin.getUserId());
        deliver.setUserName(admin1.getUserName());
        deliverMapper.insert(deliver);
        stockMapper.deleteById(deliverdto.getDrugId());
        Statistic statistic = new Statistic();
        statistic.setDrugId(deliverdto.getDrugId());
        statistic.setDeliverAmount(deliverdto.getDeliverAmount());
        String format = DateUtils.format(new Date());
        int i =format.indexOf("-");
        int start = format.indexOf("-")+1;
        int end = format.indexOf("-")+3;
        String mouth = format.substring(start, end);
        statistic.setMonth(mouth);
        statistic.setYear(DateUtils.format(new Date()).substring(0,i));





        statisticMapper.insert(statistic);





        return deliver.getId();
    }

    @Override
    public PageUtils getALl(DeliverPage deliverPage) {
        deliverPage.setCurrIndex((deliverPage.getPageNo() - 1) * deliverPage.getPageSize());
        deliverPage.setEndIndex(deliverPage.getPageSize());

        PageUtils pageUtils = new PageUtils(deliverMapper.getAllById(deliverPage), deliverMapper.getCount(deliverPage));

        return pageUtils;
    }

    @Override
    public void de(String id) {
        deliverMapper.deleteById(id);

    }

    @Override
    public void up(Deliver deliver) {
        deliverMapper.updateById(deliver);

    }
}
